package xyz.xtt.exchange.service.impl;

import static java.util.stream.Collectors.toMap;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.collection.CollectionUtil;
import xyz.xtt.asset.client.response.NftListVo;
import xyz.xtt.exchange.service.IIndexService;
import xyz.xtt.exchange.service.IInnerService;
import xyz.xtt.exchange.service.IUserLikeService;
import xyz.xtt.user.client.response.UserInfoResponse;

@Service
public class IIndexServiceImpl implements IIndexService {
	@Autowired
	private IInnerService iInnerService;
	@Autowired
	private IUserLikeService iUserLikeService;

	@Override
	public List<NftListVo> index(Long uid, Integer type, Integer currPage, Integer pageSize, String sortType, String sortOrder) {
		List<NftListVo> list = iInnerService.index(uid, type, currPage, pageSize, sortType, sortOrder);
		if (CollectionUtil.isEmpty(list)) {
			return list;
		}
		Set<Long> userIds = new HashSet<>();
		list.forEach(x -> userIds.add(x.getAuthorId()));
		List<UserInfoResponse> userInfo = iInnerService.userInfo(userIds);
		Map<Long, UserInfoResponse> userList = userInfo.stream().collect(toMap(UserInfoResponse::getAppUserId, Function.identity(), (a, b) -> b));
		// 根据用户ID 获取用户信息
		// 查询是否点赞
		Set<String> likeIds = iUserLikeService.getUserLikeCache(uid);
		list.forEach(x -> {
			x.setIsLike(likeIds.contains(x.getGoodsId()));
			x.setAuthorName(getUserField(userList, x.getAuthorId(), UserInfoResponse::getNickname));
			x.setAuthorUrl(getUserField(userList, x.getAuthorId(), UserInfoResponse::getAvatarUri));
			// 处理 公示 预售
			x.setShowStatus(getShowStatus(x));
			x.setHot(Objects.isNull(x.getHot()) ? 0 : x.getHot() * 3);
		});
		return list;
	}

	public String getUserField(Map<Long, UserInfoResponse> userMap, Long uid, Function<? super UserInfoResponse, String> mapper) {
		if (!userMap.containsKey(uid)) {
			return StringUtils.EMPTY;
		}
		UserInfoResponse tblUser = userMap.get(uid);
		return getField(tblUser, mapper);
	}

	private String getField(UserInfoResponse seller, Function<? super UserInfoResponse, String> mapper) {
		return Optional.ofNullable(seller).map(mapper).filter(StringUtils::isNotBlank).orElse(StringUtils.EMPTY);
	}

	/**
	 * 返回显示内容， 0 空， 1 公示， 2预售
	 * 
	 * @param nftListVo
	 * @return
	 */
	private Integer getShowStatus(NftListVo nftListVo) {
		long curr = System.currentTimeMillis();
		if (Objects.nonNull(nftListVo.getPublicityPeriodStart()) && Objects.nonNull(nftListVo.getPublicityPeriodEnd())) {
			if (curr >= nftListVo.getPublicityPeriodStart() && curr < nftListVo.getPublicityPeriodEnd()) {
				return 1;
			}
		}
		if (Objects.nonNull(nftListVo.getPreSellStartDatetime()) && Objects.nonNull(nftListVo.getPreSellEndDatetime())) {
			if (curr >= nftListVo.getPreSellStartDatetime() && curr < nftListVo.getPreSellEndDatetime()) {
				return 2;
			}
		}
		return 0;
	}
}
